﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Quartz;
using OpenPop.Mime;
using System.Data.SqlClient;
using System.Data;
using Common.Logging;

namespace EmailSchedule
{
    class respondEmailFromQueue:IJob
    {
        private static ILog _log = LogManager.GetLogger(typeof(respondEmailFromQueue));
        public virtual void Execute(IJobExecutionContext context)
        {
            _log.Info(string.Format("respondEmailFromQueue! - {0}", System.DateTime.Now.ToString("r")));
            if (Helper.RespondingMessageQueue.Count > 0)
            {
                try
                {
                    MessageInfo respondMessage = Helper.RespondingMessageQueue.Dequeue();
                    Helper.LogToFile(LogFileType.MESSAGE, " #" + respondMessage.MessageId,Helper.respondEmailFromQueue);
                    using (SqlConnection cn = new SqlConnection(Helper.sqlConnectString))
                    {
                        cn.Open();
                        using (SqlCommand cm = cn.CreateCommand())
                        {
                            cm.CommandText = "UPDATE_EMAIL_STATUS";
                            cm.CommandType = CommandType.StoredProcedure;
                            cm.Parameters.AddWithValue("@MessageId", respondMessage.MessageId);
                            cm.ExecuteNonQuery();
                            Helper.MessageIdList.Remove(respondMessage.MessageId);
                        }
                    }
                }
                catch (Exception ex)
                {
                    Helper.LogToFile(LogFileType.EXCEPTION, "- respondEmailFromQueue -" + ex.Message,Helper.respondEmailFromQueue);
                }
            }
        }
    }
}
